<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\User\VipExpireRemind::class,
        Commands\User\CheckVipIsExpire::class,
        Commands\PushCenter\CreateTask::class,
        Commands\Demand\ClearTop::class,
        Commands\PushCenter\SendWxNotice::class,
        Commands\Order\IntegralCashOut\TransferUser::class,
        Commands\Order\IntegralCashOut\ReturnIntegral::class,
        Commands\Task\TaskOver::class,
        Commands\Task\TaskCancelHigh::class,
        Commands\Task\TaskOverRemind::class,
        Commands\Task\TaskRejectTimeout::class,
        Commands\Task\TaskUnpassRejectTimeout::class,
        Commands\Task\TaskRequestOvertime::class,
        Commands\Task\TaskSubmitApprovalTimeout::class,
        Commands\Task\TaskOverRebackIntegral::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // 用户会员到期提醒
        $schedule->command(Commands\User\VipExpireRemind::class)->dailyAt('15:00')->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\User\VipExpireRemind::class))->runInBackground();
        
        // 用户vip过期设置为普通用户
        $schedule->command(Commands\User\CheckVipIsExpire::class)->dailyAt('00:30')->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\User\CheckVipIsExpire::class))->runInBackground();

        // 推送脚本
        $schedule->command(Commands\PushCenter\CreateTask::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\PushCenter\CreateTask::class))->runInBackground();

        // 清除置顶需求
        $schedule->command(Commands\Demand\ClearTop::class)->dailyAt('00:00')->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Demand\ClearTop::class))->runInBackground();

        // 微信消息推送
        $schedule->command(Commands\PushCenter\SendWxNotice::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\PushCenter\SendWxNotice::class))->runInBackground();

        // 积分提现
        $schedule->command(Commands\Order\IntegralCashOut\TransferUser::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Order\IntegralCashOut\TransferUser::class))->runInBackground();

        // 积分提现失败退还（每半小时执行一次）
        $schedule->command(Commands\Order\IntegralCashOut\ReturnIntegral::class)->everyThirtyMinutes()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Order\IntegralCashOut\ReturnIntegral::class))->runInBackground();

		// 判断任务是否已过期
		$schedule->command(Commands\Task\TaskOver::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskOver::class))->runInBackground();

		// 判断优质任务是否已过期
		$schedule->command(Commands\Task\TaskCancelHigh::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskCancelHigh::class))->runInBackground();

		// 判断任务是否将要结束
		$schedule->command(Commands\Task\TaskOverRemind::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskOverRemind::class))->runInBackground();

		// 判断申诉是否超时
		$schedule->command(Commands\Task\TaskRejectTimeout::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskRejectTimeout::class))->runInBackground();

		// 判断不通过记录是否超过申诉期限
		$schedule->command(Commands\Task\TaskUnpassRejectTimeout::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskUnpassRejectTimeout::class))->runInBackground();

		// 更新做单超时的报名记录
		$schedule->command(Commands\Task\TaskRequestOvertime::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskRequestOvertime::class))->runInBackground();

		// 提交任务资料超过审核时间自动标记为通过
		$schedule->command(Commands\Task\TaskSubmitApprovalTimeout::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskSubmitApprovalTimeout::class))->runInBackground();

		// 任务结束后退还冻结积分
		$schedule->command(Commands\Task\TaskOverRebackIntegral::class)->everyMinute()->withoutOverlapping()->appendOutputTo($this->getCommandLogPath(Commands\Task\TaskOverRebackIntegral::class))->runInBackground();
    }

    /**
     * @param $command
     * @return string
     */
    protected function getCommandLogPath($command)
    {
        $path = 'logs' . DIRECTORY_SEPARATOR . 'console';

        if (false !== strrpos($command, __NAMESPACE__)) {
            $path .= dirname(str_replace("\\", '/', strtolower(substr($command, strlen(__NAMESPACE__)))));
        }

        $filename = substr($command, strripos($command, "\\") + 1 ) . '.log';

        !is_dir(storage_path($path)) && mkdir(storage_path($path), 0755, true);

        return storage_path($path . DIRECTORY_SEPARATOR . $filename);

    }
}
